//
//  AllBrandListViewController.m
//  ChunLaiZhuangClient
//
//  Created by zzs on 16/4/13.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "AllBrandListViewController.h"
#import "BrandsGroupZiMuModel.h"
#import "BrandCategoryViewController.h"
@interface AllBrandListViewController (){
    BrandsGroupZiMuSubModel *groupModel;
}

@end

@implementation AllBrandListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNaviLeftButton:@""];
    [self setNaviTitle:@"全部品牌"];
    [self loadData];
    // Do any additional setup after loading the view.
}
#pragma mark-网络请求
-(void)loadData{
    [[LKDHud shareHud] showLoadingView];
    [ApiClient getBrandsGroupZiMuDidFinishLoaded:^(BrandsGroupZiMuModel *model, NSString *path) {
        [[LKDHud shareHud] hideLoadingView];
        groupModel=model.data;
        if (![self.view.subviews containsObject:self.displayView]) {
            [self.view addSubview:self.displayView];
        }

    } didFailLoaded:^(NSError *error, NSString *path) {
         [[LKDHud shareHud] hideLoadingView];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UITableView*)displayView{
    if (!_displayView) {
        _displayView=[[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
        _displayView.delegate=self;
        _displayView.dataSource=self;
        _displayView.backgroundColor=[UIColor whiteColor];
    }
    return _displayView;
}
#pragma mark-UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    BrandsGroupZiMuDetailModel *detailModel=groupModel.brand_groups[section];
    return detailModel.brands.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return groupModel.brand_groups.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                        identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
    }
    BrandsGroupZiMuDetailModel *detailModel=groupModel.brand_groups[indexPath.section];
    BrandGroupDetailSubModel *subModel=detailModel.brands[indexPath.row];
    cell.textLabel.text=subModel.name;
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 25;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    BrandsGroupZiMuDetailModel *detailModel=groupModel.brand_groups[section];
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 25)];
    label.backgroundColor = [UIColor colorWithHexString:@"ededed"];
    label.font = [UIFont systemFontOfSize:16];
    label.textColor = [UIColor colorWithHexString:@"000000"];
    label.text=[NSString stringWithFormat:@"  %@",detailModel.group];
    return label;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BrandsGroupZiMuDetailModel *detailModel=groupModel.brand_groups[indexPath.section];
    BrandGroupDetailSubModel *subModel=detailModel.brands[indexPath.row];
    BrandCategoryViewController *brandVC=[[BrandCategoryViewController alloc] init];
    [brandVC setBrandDetailModel:subModel];
    [self.navigationController pushViewController:brandVC animated:YES];
    
}


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    if (groupModel.brand_groups.count==0) {
        return nil;
    }else{
        tableView.sectionIndexBackgroundColor = [UIColor clearColor];
        tableView.sectionIndexColor = [UIColor colorWithHexString:@"007aff"];
        
        NSMutableArray* letter = [NSMutableArray array];
        for (int i = 0; i < groupModel.brand_groups.count;  i++) {
            BrandsGroupZiMuDetailModel *detailModel=groupModel.brand_groups[i];
            [letter addObject:detailModel.group];
        }
        return letter;
        
    }
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    if(!KCNSSTRING_ISEMPTY(title)){
        for (int i = 0; i < groupModel.brand_groups.count; i++) {
            BrandsGroupZiMuDetailModel *detailModel=groupModel.brand_groups[i];
            if ([title isEqualToString:detailModel.group]) {
                return i;
            }
        }
    }
    return index;
}




-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0,0, 0, 0)];
    }
    
}
-(void)viewDidLayoutSubviews
{
    if ([self.displayView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.displayView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([self.displayView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.displayView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
